package com.api.adaptor;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.daimajia.numberprogressbar.NumberProgressBar;
import com.api.base.BaseApplication;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.FalsifyFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import me.goldze.mvvmhabit.binding.command.BindingCommand;

public class ViewAdaptor {
    @BindingAdapter(value = {"is_liner","is_grid","is_stagger","column","orientation"},requireAll = false)
    public static void recycleView(RecyclerView layout, final boolean liner, final boolean grid, final boolean stagger, final int column, final int orientation){
        if (liner){
            layout.setLayoutManager(new LinearLayoutManager(BaseApplication.getContext()));
        }
        if (grid){
            layout.setLayoutManager(new GridLayoutManager(BaseApplication.getContext(),column));
        }
        if (stagger){
            layout.setLayoutManager(new StaggeredGridLayoutManager(column,orientation));
        }
    }
    @BindingAdapter(value = {"onRefreshCommand","onLoadMoreCommand"},requireAll = false)
    public static void smartRefreshCommand(final SmartRefreshLayout layout, final BindingCommand<SmartRefreshLayout> onRefreshCommand, final BindingCommand<SmartRefreshLayout> onLoadMordCommand){
        layout.setRefreshHeader(new ClassicsHeader(BaseApplication.getContext()));
        layout.setRefreshFooter(new FalsifyFooter(BaseApplication.getContext()));
        layout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (onRefreshCommand != null){
                    onRefreshCommand.execute(layout);
                }
            }
        });
        layout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (onLoadMordCommand != null){
                    onLoadMordCommand.execute(layout);
                }
            }
        });
    }

    @BindingAdapter(value = {"set_progress","set_total"},requireAll = true)
    public static void progressBarView(final NumberProgressBar layout, final int progress, final int total){
        layout.setProgress(progress);
        layout.setMax(total);
        layout.invalidate();
    }
}
